﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using CustomControls;

namespace Buccaneer
{
    public partial class AddressBar : UserControl
    {
        #region Constructor

        public AddressBar()
        {
            this.Initialize();

            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);

            g = this.CreateGraphics();

            this.separators = new List<BitmapButton>();
            this.pathParts = new List<string>();
        }

        private void Initialize()
        {
            this.centerImg = (Image)Options.GetResource("addressBar");
            this.rightImg  = (Image)Options.GetResource("addressBarRight");
            this.leftImg = (Image)Options.GetResource("addressBarLeft");

            this.Size = new Size(400, this.leftImg.Height);

            this.BackgroundImage = new Bitmap(this.centerImg, this.Size);

            g = Graphics.FromImage(this.BackgroundImage);

            g.DrawImage(this.leftImg, new Point(0, 0));
            g.DrawImage(this.rightImg, new Point(this.Width - this.rightImg.Width, 0));
            for (int i = this.leftImg.Width; i < (int)((this.Size.Width - this.rightImg.Width) / this.centerImg.Width); i++)
            {
                g.DrawImage(this.centerImg, new Point(i * this.centerImg.Width, 0));
            }
            g.DrawImage(this.centerImg, new Point(this.Width - this.rightImg.Width - this.centerImg.Width, 0));
            
            this.textBox = new TextBox();
            this.textBox.Multiline = true;
            this.textBox.BorderStyle = BorderStyle.None;
            this.textBox.Location = new Point(this.leftImg.Width, this.leftImg.Width);
            this.textBox.Size = new Size(this.Width - this.leftImg.Width - this.rightImg.Width, this.Height - this.leftImg.Width - 3);
            this.textBox.Font = Options.Font;

            this.textBox.KeyUp += new KeyEventHandler(AddressBar_KeyUp);
            this.textBox.TextChanged += new EventHandler(textBox_TextChanged);
            this.SizeChanged += new EventHandler(AddressBar_SizeChanged);
            this.MouseClick += new MouseEventHandler(AddressBar_MouseClick);

            this.Controls.Add(this.textBox);
        }

        #endregion

        #region OnPaint

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            /*/
            g.DrawImage(
                picture,
                new Point(
                    (int)((this.Size.Width - (this.Size.Height - (int)(.3 * this.Size.Height))) / 2),
                    (int)(.1 * this.Size.Height)
                )
            );
            //* /
            g.DrawString(
                text,
                Options.Font,
                Brushes.Black,
                new Rectangle(
                    new Point(0, 0),
                    this.Size
                ),
                strFormat
            );
            //*/
        }

        #endregion

        #region Private Attributes

        Graphics g;

        List<BitmapButton> separators; 
        static Image normal, hovered, clicked, focussed, disabled, error;
        string text;
        Image leftImg, centerImg, rightImg, picture;
        TextBox textBox;
        List<string> pathParts;
        
        #endregion

        #region Public Attributes

        public string Text
        {
            get
            {
                return String.Join("\\", this.pathParts.ToArray());
            }
            set
            {
                this.pathParts = new List<string>();
                this.pathParts.Clear();
                this.pathParts.AddRange(value.Split(new string[] { "\\" }, StringSplitOptions.None));
                this.UpdatePath();
            }
        }

        #endregion

        #region Private Functions

        #endregion

        #region Public Functions

        #endregion

        #region Private Eventhandlers

        void textBox_TextChanged(object sender, EventArgs e)
        {
            
        }

        void AddressBar_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                this.textBox.Text = this.textBox.Text.Replace(Environment.NewLine, "\\");
                this.textBox.Select(this.textBox.Text.Length, 0);
                this.Text = textBox.Text;
                UpdatePath();
            }
            else if (e.KeyData == Keys.Back)
            {

            }

            this.Invalidate(true);
            this.Update();

            this.OnKeyUp(e);
        }

        void AddressBar_MouseClick(object sender, MouseEventArgs e)
        {
            this.textBox.Select(this.textBox.Text.Length, 0);
        }

        void AddressBar_SizeChanged(object sender, EventArgs e)
        {
            this.textBox.Size = new Size(this.Width - this.leftImg.Width - this.rightImg.Width, this.Height - this.leftImg.Width - 3);
            this.BackgroundImage = new Bitmap(this.centerImg, this.Size);

            g = Graphics.FromImage(this.BackgroundImage);

            g.Clear(Color.White);
            g.DrawImage(this.leftImg, new Point(0, 0));
            g.DrawImage(this.rightImg, new Point(this.Width - this.rightImg.Width, 0));
            for (int i = this.leftImg.Width / this.centerImg.Width; i < (int)((this.Size.Width - this.rightImg.Width) / this.centerImg.Width); i++)
            {
                g.DrawImage(this.centerImg, new Point(i * this.centerImg.Width, 0));
            }
            g.DrawImage(this.centerImg, new Point(this.Width - this.rightImg.Width - this.centerImg.Width, 0));
        }

        #endregion
    }
}
